# Summary: Creates Appendix C

######################################################
#-------------------Appendix C ----------------------#
######################################################

options(scipen=999)
rm(list = ls())

# Load libraries
library(rddensity)
library(rdrobust)
library(sandwich)
library(lmtest)

# Set WD
# setwd('~/Dataverse/')

# Source functions
source('./Code/fun_analysis.R')

# Load Data
load(file = "./Data/d.RData")

# Keep only observations from municipalities where both parties had male and female candidates
d1 <- subset(d, nonZeroDF == 2)
length(unique(d1$codeyear))

# Get codeyear for where a centralized and decentralized party won
centParties <- d1$codeyear[(d1$SIGLA_PARTIDO %in% c("PC do B", "PT", "PSDB", "PPS", "PFL", "DEM", "PL", "PR", "PDT")) & d1$eleito == 1]
decentParties <- d1$codeyear[!(d1$SIGLA_PARTIDO %in% c("PC do B", "PT", "PSDB", "PPS", "PFL", "DEM", "PL", "PR", "PDT")) & d1$eleito == 1]

# Centralized and Descentralized parties
d1$centralized <- as.numeric(d1$SIGLA_PARTIDO %in% c("PC do B", "PT", "PSDB", "PPS", "PFL", "DEM", "PL", "PR", "PDT"))

###########################################################################
#-------------------Appendix C.1: Descriptive Stats ----------------------#
###########################################################################

# TABLE C.1.
sink("./AppendixCResults/tableC1.txt")
print("TABLE C.1: Descriptive Statistics")
summary(d1$avg_DF); length(d1$avg_DF); sd(d1$avg_DF)
summary(d1$avg_DF[d1$codeyear %in% centParties]); length(d1$avg_DF[d1$codeyear %in% centParties]); sd(d1$avg_DF[d1$codeyear %in% centParties])
summary(d1$avg_DF[d1$codeyear %in% decentParties]); length(d1$avg_DF[d1$codeyear %in% decentParties]); sd(d1$avg_DF[d1$codeyear %in% decentParties])
sink()

######################################################################
#-------------------Appendix C.2: Manipulation ----------------------#
######################################################################

run_var <- na.omit(d1$run_var)
run_varT2 <- run_var[d1$codeyear %in% centParties]
run_varT3 <- run_var[d1$codeyear %in% decentParties]
summary(t1 <- rddensity(X = run_var, c = 0)) # With massPoints
summary(t2 <- rddensity(X = run_varT2, c = 0)) # With massPoints
summary(t3 <- rddensity(X = run_varT3, c = 0)) # With massPoints

# Result: No sorting
# Density Plot 
# FIGURE C.1.
pdf('./AppendixCResults/figC1.pdf', width = 8, height = 8)
par(mar = c(5, 5, 2, 2))
hist(run_var[abs(run_var)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
     main = "", xlab = "Difference in Vote Share", cex.lab = 1.5, cex.axis = 1.5)
abline(v = 0, col = "red", lwd = 1.5)
text(x = 8.5, y = 415, labels = paste("p-value = ", round(t1$test$p_jk, 4)), cex = 1.25)
par(mar = c(5, 5, 2, 2))
hist(run_varT2[abs(run_varT2)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
     main = "", xlab = "Difference in Vote Share", cex.lab = 1.5, cex.axis = 1.5)
abline(v = 0, col = "red", lwd = 1.5)
text(x = 8.5, y = 200, labels = paste("p-value = ", signif(t2$test$p_jk, 4)), cex = 1.25)
par(mar = c(5, 5, 2, 2))
hist(run_varT3[abs(run_varT3)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
     main = "", xlab = "Difference in Vote Share", cex.lab = 1.5, cex.axis = 1.5)
abline(v = 0, col = "red", lwd = 1.5)
text(x = 8.5, y = 200, labels = paste("p-value = ", signif(t3$test$p_jk, 4)), cex = 1.25)
dev.off()


######################################################################
#-------------------Appendix C.3: Balance Test ----------------------#
######################################################################

################################
########## Municipal-Party Level
################################

#####--------------- Lag DV ---------------##### 
# % of vote share male
d$temp <- ifelse(is.na(d$perc_party_mun_fem_placebo1DF), 0, d$perc_party_mun_fem_placebo1DF)
d$perc_party_mun_male_placebo1DF <- d$perc_party_mun_placebo1DF - d$temp
# N of male candidates
d$temp <- ifelse(is.na(d$n_party_Femcandidates_placebo1DF), 0, d$n_party_Femcandidates_placebo1DF)
d$n_party_Malecandidates_placebo1DF <- d$n_party_candidates_placebo1DF - d$n_party_Femcandidates_placebo1DF
# Avg vote share male
d$avg_placebo1DF <- d$perc_party_mun_placebo1DF/(d$n_party_candidates_placebo1DF)
# Avg vote share male
d$avg_male_placebo1DF <- d$perc_party_mun_male_placebo1DF/(d$n_party_Malecandidates_placebo1DF)
# Avg vote share female
d$avg_fem_placebo1DF <- d$perc_party_mun_fem_placebo1DF/(d$n_party_Femcandidates_placebo1DF)

# Find municipalities that should be included (both parties had male and female candidates)
valid <- aggregate(d$n_party_Malecandidates_placebo1DF > 0 & d$n_party_Femcandidates_placebo1DF>0 & (!is.na(d$avg_male_placebo1DF) & !is.na(d$avg_fem_placebo1DF)), list(d$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance <- subset(d, codeyear %in% valid_place & nonZeroDF == 2)

######### All Candidates
b1 = rdrobust(y = dBalance$avg_placebo1DF,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b1)
b2 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_placebo1DF,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b2)
b3 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$avg_placebo1DF,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b3)

######### All Women Candidates
b4 = rdrobust(y = dBalance$avg_fem_placebo1DF,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b4)
b5 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_fem_placebo1DF,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b5)
b6 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$avg_fem_placebo1DF,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b6)


#####--------------- Lag DV State Deputy---------------##### 
# % of vote share male
d$temp <- ifelse(is.na(d$perc_party_mun_fem_placebo1DE), 0, d$perc_party_mun_fem_placebo1DE)
d$perc_party_mun_male_placebo1DE <- d$perc_party_mun_placebo1DE - d$temp
# N of male candidates
d$temp <- ifelse(is.na(d$n_party_Femcandidates_placebo1DE), 0, d$n_party_Femcandidates_placebo1DE)
d$n_party_Malecandidates_placebo1DE <- d$n_party_candidates_placebo1DE - d$n_party_Femcandidates_placebo1DE
# Avg vote share male
d$avg_placebo1DE <- d$perc_party_mun_placebo1DE/(d$n_party_candidates_placebo1DE)
# Avg vote share male
d$avg_male_placebo1DE <- d$perc_party_mun_male_placebo1DE/(d$n_party_Malecandidates_placebo1DE)
# Avg vote share female
d$avg_fem_placebo1DE <- d$perc_party_mun_fem_placebo1DE/(d$n_party_Femcandidates_placebo1DE)

# Find municipalities that should be included (both parties had male and female candidates)
valid <- aggregate(d$n_party_Malecandidates_placebo1DE > 0 & d$n_party_Femcandidates_placebo1DE>0 & (!is.na(d$avg_male_placebo1DE) & !is.na(d$avg_fem_placebo1DE)), list(d$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance <- subset(d, codeyear %in% valid_place & nonZeroDF == 2)

######### All Candidates
b7 = rdrobust(y = dBalance$avg_placebo1DE,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b7)
b8 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_placebo1DE,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b8)
b9 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$avg_placebo1DE,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b9)


######### Women Candidates
b10 = rdrobust(y = dBalance$avg_fem_placebo1DE,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b10)
b11 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_fem_placebo1DE,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b11)
b12 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$avg_fem_placebo1DE,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b12)

#####--------------- Local Council---------------##### 

load(file = "./Data/d_balance.RData")

# Combine datasets
d <- merge(d, d_balance, by = c('codeyear', 'NUMERO_PARTIDO'), all.x = TRUE)

#### Variable for balance test
# Perct of vote share
d$perc_party_mun_placebo_Ver <- (d$total_votes_party_vereador/d$total_votes_district_vereador) * 100
d$perc_party_mun_fem_placebo_Ver <- (d$total_votes_party_fem_vereador/d$total_votes_district_vereador) * 100
d$temp <- ifelse(is.na(d$perc_party_mun_fem_placebo_Ver), 0, d$perc_party_mun_fem_placebo_Ver)
d$perc_party_mun_male_placebo_Ver <- d$perc_party_mun_placebo_Ver - d$temp 
# N of Candidates
d$temp <- ifelse(is.na(d$n_candidates_fem_vereador), 0, d$n_candidates_fem_vereador)
d$n_candidates_male_vereador <- d$n_candidates_vereador - d$temp
# Avg Vote Sahre
d$avg_vs_ver <- (d$perc_party_mun_placebo_Ver/d$n_candidates_vereador)
d$avg_vs_male_ver <- (d$perc_party_mun_male_placebo_Ver/d$n_candidates_male_vereador) 
d$avg_vs_fem_ver <- (d$perc_party_mun_fem_placebo_Ver/d$n_candidates_fem_vereador) 

# Keep municipalities that had men and women running  
valid <- aggregate((d$n_candidates_vereador - d$n_candidates_fem_vereador) > 0 & d$n_candidates_fem_vereador>0 & (!is.na(d$avg_vs_male_ver) & !is.na(d$avg_vs_fem_ver)), list(d$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance <- subset(d, codeyear %in% valid_place & nonZeroDF == 2)

######### All Candidates
b13 = rdrobust(y = dBalance$avg_vs_ver,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b13)
b14 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_vs_ver,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b14)
b15 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$avg_vs_ver,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b15)

######### Women Candidates
b16 = rdrobust(y = dBalance$avg_vs_fem_ver,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b16)
b17 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_vs_fem_ver,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b17)
b18 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$avg_vs_fem_ver,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b18)

# TABLE C.2
ordering = c(1,7,13,4,10,16,2,8,14,5,11,17,3,9,15,6,12,18)
eval(parse(text = paste0("Estimate = rbind(", paste0(paste0("b", ordering, "[['coef']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("p_value = rbind(", paste0(paste0("b", ordering, "[['pv']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("h = rbind(", paste0(paste0("b", ordering, "[['bws']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_co = rbind(", paste0(paste0("b", ordering, "[['N_h']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_tr = rbind(", paste0(paste0("b", ordering, "[['N_h']][2]"), collapse = ","), ")") 
))
table_c2 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixCResults/tableC2.txt")
print("TABLE C.2: Municipal-Party Level Tests, All Three Regimes")
table_c2
sink()

################################
########## Individual Level
################################
load(file = "./Data/balance_ind.RData")

### Add variables
# Note: Unfortunately, we had to use a new version of TSE data in order to create
# the variables for the balance tests. As a result, our sample is slightly different
# Our results are robust to the use of this different sample. Also, these data are
# only available for elections after 1996.
dBalance <- merge(d1, balance_ind, 
              by.x = c('ANO_ELEICAO', 'CODIGO_MUNICIPIO', 
                       'NUMERO_PARTIDO'),
              by.y = c('ANO_ELEICAO', 'SG_UE', 
                      'NR_PARTIDO'))
dBalance$women <- as.numeric(dBalance$CODIGO_SEXO == 4)

# Keep only municipalities that both candidates are available
dBalance$x <- 1
valid <- aggregate(dBalance$x, list(dBalance$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance <- subset(dBalance, codeyear %in% valid_place & nonZeroDF == 2)

dBalance$held_office <- dBalance$held_office > 0
dBalance$ran_office <- dBalance$ran_office > 0

# TABLE C.3
######### All Parties
b1 = rdrobust(y = dBalance$business_occ,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b1)
b2 = rdrobust(y = dBalance$health_occ,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b2)
b3 = rdrobust(y = dBalance$education_occ,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b3)
b4 = rdrobust(y = dBalance$merchant_occ,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b4)
b5 = rdrobust(y = dBalance$farmer_occ,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b5)
b6 = rdrobust(y = dBalance$pubservant_occ,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b6)
b7 = rdrobust(y = dBalance$laywer_pcc,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b7)
b8 = rdrobust(y = dBalance$incumbent,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b8)
b9 = rdrobust(y = dBalance$held_office,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b9)
b10 = rdrobust(y = dBalance$ran_office,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b10)
# Age is missing for one candidate in one municipality, we remove it to keep the 
# data balanced. 
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b11 = rdrobust(y = dBalance$age[dBalance$codeyear %in% keep],
                 x = dBalance$run_var[dBalance$codeyear %in% keep],
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear[dBalance$codeyear %in% keep])
summary(b11)
b12 = rdrobust(y = dBalance$married,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b12)
b13 = rdrobust(y = dBalance$highschool,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b13)
b14 = rdrobust(y = dBalance$somecollege,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b14)
b15 = rdrobust(y = dBalance$women,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b15)
b16 = rdrobust(y = dBalance$centralized,
                 x = dBalance$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = dBalance$codeyear)
summary(b16)

######### Centralized Parties
b17 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$business_occ,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b17)
b18 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$health_occ,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b18)
b19 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$education_occ,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b19)
b20 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$merchant_occ,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b20)
b21 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$farmer_occ,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b21)
b22 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$pubservant_occ,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b22)
b23 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$laywer_pcc,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b23)
b24 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$incumbent,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b24)
b25 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$held_office,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b25)
b26 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$ran_office,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b26)
keep <- names(table(subset(dBalance, codeyear %in% centParties)$codeyear[!is.na(subset(dBalance, codeyear %in% centParties)$age)])[table(subset(dBalance, codeyear %in% centParties)$codeyear[!is.na(subset(dBalance, codeyear %in% centParties)$age)])==2])
b27 = rdrobust(y = subset(dBalance, codeyear %in% centParties & codeyear %in% keep)$age,
                 x = subset(dBalance, codeyear %in% centParties & codeyear %in% keep)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties & codeyear %in% keep)$codeyear)
summary(b27)
b28 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$married,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b28)
b29 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$highschool,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b29)
b30 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$somecollege,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b30)
b31 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$women,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b31)

######### Decentralized Parties
b32 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$business_occ,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b32)
b33 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$health_occ,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b33)
b34 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$education_occ,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b34)
b35 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$merchant_occ,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b35)
b36 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$farmer_occ,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b36)
b37 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$pubservant_occ,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b37)
b38 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$laywer_pcc,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b38)
b39 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$incumbent,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b39)
b40 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$held_office,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b40)
b41 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$ran_office,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b41)
keep <- names(table(subset(dBalance, codeyear %in% decentParties)$codeyear[!is.na(subset(dBalance, codeyear %in% decentParties)$age)])[table(subset(dBalance, codeyear %in% decentParties)$codeyear[!is.na(subset(dBalance, codeyear %in% decentParties)$age)])==2])
b42 = rdrobust(y = subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$age,
                 x = subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$codeyear)
summary(b42)
b43 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$married,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b43)
b44 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$highschool,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b44)
b45 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$somecollege,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b45)
b46 = rdrobust(y = subset(dBalance, codeyear %in% decentParties)$women,
                 x = subset(dBalance, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties)$codeyear)
summary(b46)

# Campaign Expenditures are only available after 2000 and many candidates do not report
# So, we will do a separate analysis
load(file = './Data/DespesaMunicipal.RData')

# Add Data
dBalance2 <- merge(dBalance, despesa, 
                   by.x = c('CODIGO_MUNICIPIO', 'ANO_ELEICAO', 'NUMERO_PARTIDO'), 
                   by.y = c('SG_UE', 'ANO_ELEICAO', 'NR_PART'))
# Keep only municipalities that both candidates are available
dBalance2$x <- 1
valid <- aggregate(dBalance2$x, list(dBalance2$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance2 <- subset(dBalance2, codeyear %in% valid_place & nonZeroDF == 2)

# Rescale by 1000
dBalance2$DESPESA <- dBalance2$DESPESA/1000

b47 = rdrobust(y = subset(dBalance2)$DESPESA,
                 x = subset(dBalance2)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2)$codeyear)
summary(b47)
b48 = rdrobust(y = subset(dBalance2, codeyear %in% centParties)$DESPESA,
                 x = subset(dBalance2, codeyear %in% centParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2, codeyear %in% centParties)$codeyear)
summary(b48)
b49 = rdrobust(y = subset(dBalance2, codeyear %in% decentParties)$DESPESA,
                 x = subset(dBalance2, codeyear %in% decentParties)$run_var,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2, codeyear %in% decentParties)$codeyear)
summary(b49)

# TABLE C.3
ordering = c(1:15,47,16:31,48,32:46,49)

eval(parse(text = paste0("Estimate = rbind(", paste0(paste0("b", ordering, "[['coef']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("p_value = rbind(", paste0(paste0("b", ordering, "[['pv']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("h = rbind(", paste0(paste0("b", ordering, "[['bws']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_co = rbind(", paste0(paste0("b", ordering, "[['N_h']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_tr = rbind(", paste0(paste0("b", ordering, "[['N_h']][2]"), collapse = ","), ")") 
))
table_c3 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixCResults/tableC3.txt")
print("TABLE C.3: Candidate Level Tests, All Three Regimes")
table_c3
sink()


# TABLE C.4
# We found imbalance in terms of health and educational occupations. We will add them as covariates
b1 = rdrobust(y = subset(dBalance, codeyear %in% centParties)$avg_DF,
                 x = subset(dBalance, codeyear %in% centParties)$run_var,
                 covs = cbind(subset(dBalance, codeyear %in% centParties)$health_occ,
                              subset(dBalance, codeyear %in% centParties)$education_occ),
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% centParties)$codeyear)
summary(b1)

keep <- names(table(subset(dBalance, codeyear %in% decentParties)$codeyear)[table(subset(dBalance, codeyear %in% decentParties)$codeyear[!is.na(subset(dBalance, codeyear %in% decentParties)$age)])== 2])
b2 = rdrobust(y = subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$avg_DF,
                 x = subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$run_var,
                 covs = cbind(subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$age,
                              subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$married),
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance, codeyear %in% decentParties & codeyear %in% keep)$codeyear)
summary(b2)

# TABLE C.4
ordering = c(1:2)
eval(parse(text = paste0("Estimate = rbind(", paste0(paste0("b", ordering, "[['coef']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("p_value = rbind(", paste0(paste0("b", ordering, "[['pv']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("h = rbind(", paste0(paste0("b", ordering, "[['bws']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_co = rbind(", paste0(paste0("b", ordering, "[['N_h']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_tr = rbind(", paste0(paste0("b", ordering, "[['N_h']][2]"), collapse = ","), ")") 
))
table_c4 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixCResults/tableC4.txt")
print("TABLE C.4: RD Effect of Co-Partisan Mayor on Average Vote Share—Brazilian Federal Elections,
1998-2018 (Including Covariates that are imbalanced)")
table_c4
sink()


######################################################################
#-------------------Appendix C.4: Sensitivity ----------------------#
######################################################################

# FIGURE C.2
pdf("./AppendixCResults/figC2.pdf")
sensitivity(mydata = subset(d1), 
            DV = 'avg_DF', 
            ylb = -2, 
            yub = 2, 
            dvName = 'Federal Deputy', 
            clusterVar = subset(d1)$codeyear)
sensitivity(mydata = subset(d1, codeyear %in% centParties), 
            DV = 'avg_DF', 
            ylb = -2, 
            yub = 2, 
            dvName = 'Federal Deputy - Men', 
            clusterVar = subset(d1, codeyear %in% centParties)$codeyear)
sensitivity(mydata = subset(d1, codeyear %in% decentParties), 
            DV = 'avg_DF', 
            ylb = -2, 
            yub = 2, 
            dvName = 'Federal Deputy', 
            clusterVar = subset(d1, codeyear %in% decentParties)$codeyear)
dev.off()	


###############################################################################
#-------------------Appendix C.5: Different Polynomials ----------------------#
###############################################################################

# TABLE C.5
# Main Models: p = 2
out.opt.p2 <- rdrobust(y = subset(d1)$avg_DF
                       , x = subset(d1)$run_var
                       , p = 2
                       , c = 0	
                       , cluster = subset(d1)$codeyear
                       , all = TRUE)
out.cent.opt.p2 <- rdrobust(y = subset(d1, codeyear %in% centParties)$avg_DF
                            , x = subset(d1, codeyear %in% centParties)$run_var
                            , p = 2
                            , c = 0	
                            , cluster = subset(d1, codeyear %in% centParties)$codeyear
                            , all = TRUE)
out.decent.opt.p2 <- rdrobust(y = subset(d1, codeyear %in% decentParties)$avg_DF
                              , x = subset(d1, codeyear %in% decentParties)$run_var
                              , p = 2
                              , c = 0	
                              , cluster = subset(d1, codeyear %in% decentParties)$codeyear
                              , all = TRUE)
summary(out.opt.p2)
summary(out.cent.opt.p2)
summary(out.decent.opt.p2)

# Main Models: p = 3
out.opt.p3 <- rdrobust(y = subset(d1)$avg_DF
                       , x = subset(d1)$run_var
                       , p = 3
                       , c = 0	
                       , cluster = subset(d1)$codeyear
                       , all = TRUE)
out.cent.opt.p3 <- rdrobust(y = subset(d1, codeyear %in% centParties)$avg_DF
                            , x = subset(d1, codeyear %in% centParties)$run_var
                            , p = 3
                            , c = 0	
                            , cluster = subset(d1, codeyear %in% centParties)$codeyear
                            , all = TRUE)
out.decent.opt.p3 <- rdrobust(y = subset(d1, codeyear %in% decentParties)$avg_DF
                              , x = subset(d1, codeyear %in% decentParties)$run_var
                              , p = 3
                              , c = 0	
                              , cluster = subset(d1, codeyear %in% decentParties)$codeyear
                              , all = TRUE)
summary(out.opt.p3)
summary(out.cent.opt.p3)
summary(out.decent.opt.p3)

# Main Models: p = 4
out.opt.p4 <- rdrobust(y = subset(d1)$avg_DF
                       , x = subset(d1)$run_var
                       , p = 4
                       , c = 0	
                       , cluster = subset(d1)$codeyear
                       , all = TRUE)
out.cent.opt.p4 <- rdrobust(y = subset(d1, codeyear %in% centParties)$avg_DF
                            , x = subset(d1, codeyear %in% centParties)$run_var
                            , p = 4
                            , c = 0	
                            , cluster = subset(d1, codeyear %in% centParties)$codeyear
                            , all = TRUE)
out.decent.opt.p4 <- rdrobust(y = subset(d1, codeyear %in% decentParties)$avg_DF
                              , x = subset(d1, codeyear %in% decentParties)$run_var
                              , p = 4
                              , c = 0	
                              , cluster = subset(d1, codeyear %in% decentParties)$codeyear
                              , all = TRUE)
summary(out.opt.p4)
summary(out.cent.opt.p4)
summary(out.decent.opt.p4)

Estimate = rbind(out.opt.p2$coef[[3]],out.opt.p3$coef[[3]],out.opt.p4$coef[[3]],
                 out.cent.opt.p2$coef[[3]],out.cent.opt.p3$coef[[3]],out.cent.opt.p4$coef[[3]],
                 out.decent.opt.p2$coef[[3]],out.decent.opt.p3$coef[[3]],out.decent.opt.p4$coef[[3]])
p_value = rbind(out.opt.p2$pv[[3]],out.opt.p3$pv[[3]],out.opt.p4$pv[[3]],
                           out.cent.opt.p2$pv[[3]],out.cent.opt.p3$pv[[3]],out.cent.opt.p4$pv[[3]],
                           out.decent.opt.p2$pv[[3]],out.decent.opt.p3$pv[[3]],out.decent.opt.p4$pv[[3]])
h = rbind(out.opt.p2$bws[[1]],out.opt.p3$bws[[1]],out.opt.p4$bws[[1]],
                out.cent.opt.p2$bws[[1]],out.cent.opt.p3$bws[[1]],out.cent.opt.p4$bws[[1]],
                out.decent.opt.p2$bws[[1]],out.decent.opt.p3$bws[[1]],out.decent.opt.p4$bws[[1]])
n_co = rbind(out.opt.p2$N_h[[1]],out.opt.p3$N_h[[1]],out.opt.p4$N_h[[1]],
          out.cent.opt.p2$N_h[[1]],out.cent.opt.p3$N_h[[1]],out.cent.opt.p4$N_h[[1]],
          out.decent.opt.p2$N_h[[1]],out.decent.opt.p3$N_h[[1]],out.decent.opt.p4$N_h[[1]])
n_tr = rbind(out.opt.p2$N_h[[2]],out.opt.p3$N_h[[2]],out.opt.p4$N_h[[2]],
             out.cent.opt.p2$N_h[[2]],out.cent.opt.p3$N_h[[2]],out.cent.opt.p4$N_h[[2]],
             out.decent.opt.p2$N_h[[2]],out.decent.opt.p3$N_h[[2]],out.decent.opt.p4$N_h[[2]])
table_c5 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixCResults/tableC5.txt")
print("TABLE C.5: RD Effect of Co-Partisan Mayor on Average Vote Share (Different Polynomials)—
Brazilian Federal Elections, 1998-2018")
table_c5
sink()


########################################################################
#-------------------Appendix C.6: Previously Ran ----------------------#
########################################################################


# N of male candidates
d1$n_party_candidates_male_candBefore_DF <- d1$n_party_candidates_candBefore_DF - d1$n_party_Femcandidates_candBefore_DF
valid <- aggregate(d1$n_party_candidates_male_candBefore_DF > 0 & d1$n_party_Femcandidates_candBefore_DF>0, list(d1$codeyear), sum)
valid_ranB <- na.omit(valid$Group.1[valid$x == 2])
# % of vote share male
d1$perc_party_mun_male_candBeforeDF <- d1$perc_party_mun_candBefore_DF - d1$perc_party_mun_fem_candBefore_DF
# Avg vote share all
d1$avg_candBefore_DF <- d1$perc_party_mun_candBefore_DF/d1$n_party_candidates_candBefore_DF
# Subset data
d2 <- subset(d1, codeyear %in% valid_ranB)

# TABLE C.6
# Main Models: p = 2
out.opt <- rdrobust(y = subset(d2)$avg_candBefore_DF
                    , x = subset(d2)$run_var
                    , p = 1
                    , c = 0	
                    , cluster = subset(d2)$codeyear
                    , all = TRUE)
out.cent.opt <- rdrobust(y = subset(d2, codeyear %in% centParties)$avg_candBefore_DF
                         , x = subset(d2, codeyear %in% centParties)$run_var
                         , p = 1
                         , c = 0	
                         , cluster = subset(d2, codeyear %in% centParties)$codeyear
                         , all = TRUE)
out.decent.opt <- rdrobust(y = subset(d2, codeyear %in% decentParties)$avg_candBefore_DF
                           , x = subset(d2, codeyear %in% decentParties)$run_var
                           , p = 1
                           , c = 0	
                           , cluster = subset(d2, codeyear %in% decentParties)$codeyear
                           , all = TRUE)
summary(out.opt)
summary(out.cent.opt)
summary(out.decent.opt)

Estimate = rbind(out.opt$coef[[3]],out.cent.opt$coef[[3]],out.decent.opt$coef[[3]])
p_value = rbind(out.opt$pv[[3]],out.cent.opt$pv[[3]],out.decent.opt$pv[[3]])
h = rbind(out.opt$bws[[1]],out.cent.opt$bws[[1]],out.decent.opt$bws[[1]])
n_co = rbind(out.opt$N_h[[1]],out.cent.opt$N_h[[1]],out.decent.opt$N_h[[1]])
n_tr = rbind(out.opt$N_h[[2]],out.cent.opt$N_h[[2]],out.decent.opt$N_h[[2]])
table_c6 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixCResults/tableC6.txt")
print("TABLE C.6: RD Effect of Co-Partisan Mayor on Average Vote Share (Only Candidates Who Previ-
ously Ran)—Brazilian Federal Elections, 1998-2018")
table_c6
sink()


########################################################################
#-------------------Appendix C.7: Previously Won ----------------------#
########################################################################

# ##### Candidate who won before
d1$perc_party_mun_fem_wonBefore_DF <- (d1$total_party_mun_fem_wonBefore_DF/d1$total_mun_wonBefore_DF) * 100
# # % of vote share male
d1$perc_party_mun_male_wonBefore_DF <- d1$perc_party_mun_wonBefore_DF - d1$perc_party_mun_fem_wonBefore_DF
# # N of male candidates
d1$n_party_candidates_male_wonBefore_DF <- d1$n_party_candidates_wonBefore_DF - d1$n_party_Femcandidates_wonBefore_DF
# # Avg vote share 
d1$avg_wonBefore_DF <- d1$perc_party_mun_wonBefore_DF/d1$n_party_candidates_wonBefore_DF
# # Avg vote share male
d1$avg_male_wonBefore_DF <- d1$perc_party_mun_male_wonBefore_DF/d1$n_party_candidates_male_wonBefore_DF
# # Avg vote share female
d1$avg_fem_wonBefore_DF <- d1$perc_party_mun_fem_wonBefore_DF/(d1$n_party_Femcandidates_wonBefore_DF)

# # Find municipalities that should be included
valid <- aggregate(d1$n_party_candidates_male_wonBefore_DF > 0 & d1$n_party_Femcandidates_wonBefore_DF>0, list(d1$codeyear), sum)
valid_wonB <- na.omit(valid$Group.1[valid$x == 2])

# Subset data
d2 <- subset(d1, codeyear %in% valid_wonB)

# TABLE C.7
# Main Models: p = 2
out.opt <- rdrobust(y = subset(d2)$avg_wonBefore_DF
                    , x = subset(d2)$run_var
                    , p = 1
                    , c = 0	
                    , cluster = subset(d2)$codeyear
                    , all = TRUE)
out.cent.opt <- rdrobust(y = subset(d2, codeyear %in% centParties)$avg_wonBefore_DF
                         , x = subset(d2, codeyear %in% centParties)$run_var
                         , p = 1
                         , c = 0	
                         , cluster = subset(d2, codeyear %in% centParties)$codeyear
                         , all = TRUE)
out.decent.opt <- rdrobust(y = subset(d2, codeyear %in% decentParties)$avg_wonBefore_DF
                           , x = subset(d2, codeyear %in% decentParties)$run_var
                           , p = 1
                           , c = 0	
                           , cluster = subset(d2, codeyear %in% decentParties)$codeyear
                           , all = TRUE)
summary(out.opt)
summary(out.cent.opt)
summary(out.decent.opt)

Estimate = rbind(out.opt$coef[[3]],out.cent.opt$coef[[3]],out.decent.opt$coef[[3]])
p_value = rbind(out.opt$pv[[3]],out.cent.opt$pv[[3]],out.decent.opt$pv[[3]])
h = rbind(out.opt$bws[[1]],out.cent.opt$bws[[1]],out.decent.opt$bws[[1]])
n_co = rbind(out.opt$N_h[[1]],out.cent.opt$N_h[[1]],out.decent.opt$N_h[[1]])
n_tr = rbind(out.opt$N_h[[2]],out.cent.opt$N_h[[2]],out.decent.opt$N_h[[2]])
table_c7 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixCResults/tableC7.txt")
print("TABLE C.7: RD Effect of Co-Partisan Mayor on Average Vote Share (Only Candidates Who Previ-
ously Won)—Brazilian Federal Elections, 1998-2018")
table_c7
sink()

####################################################################
#-------------------Appendix C.8: RD Linear  ----------------------#
####################################################################

# Objects to store results
coefAll <- coefCen <- coefDec <- c()
seAll <- seCen <- seDec <- c()
i <- 1
d1$winner <- as.numeric(d1$run_var > 0)
for(bw in seq(0.5, 100, 0.5)){
  m1 <- lm(avg_DF ~ winner + run_var, data = subset(d1, abs(run_var) < bw ))
  m2 <- lm(avg_DF ~ winner + run_var, data = subset(d1, abs(run_var) < bw & codeyear %in% centParties))
  m3 <- lm(avg_DF ~ winner + run_var, data = subset(d1, abs(run_var) < bw & codeyear %in% decentParties))
  # Before 2008
  coefAll[i] <- coeftest(m1, vcovCL(m1, cluster = subset(d1, abs(run_var) < bw )$codeyear))['winner', 1]
  coefCen[i] <- coeftest(m2, vcovCL(m2, cluster = subset(d1, abs(run_var) < bw & codeyear %in% centParties)$codeyear))['winner', 1]
  coefDec[i] <- coeftest(m3, vcovCL(m3, cluster = subset(d1, abs(run_var) < bw & codeyear %in% decentParties)$codeyear))['winner', 1]
  seAll[i] <- sqrt(diag(vcovCL(m1, cluster = subset(d1, abs(run_var) < bw )$codeyear)))['winner']
  seCen[i] <- sqrt(diag(vcovCL(m2, cluster = subset(d1, abs(run_var) < bw & codeyear %in% centParties)$codeyear)))['winner']
  seDec[i] <- sqrt(diag(vcovCL(m3, cluster = subset(d1, abs(run_var) < bw & codeyear %in% decentParties)$codeyear)))['winner']
  # iterate
  i <- i + 1
}
lbAllBefore <- seAll * qnorm(0.025) + coefAll
ubAllBefore <- seAll * qnorm(0.975) + coefAll
lbCenBefore <- seCen * qnorm(0.025) + coefCen
ubCenBefore <- seCen * qnorm(0.975) + coefCen
lbDecBefore <- seDec * qnorm(0.025) + coefDec
ubDecBefore <- seDec * qnorm(0.975) + coefDec

# FIGURE C.8
pdf('./AppendixCResults/figC3.pdf')
plotTtest(betas = coefAll, 
          lb = lbAllBefore, 
          ub = ubAllBefore,
          ylb = -1, 
          yub = 1,  
          dvName = 'Federal Deputy')
plotTtest(betas = coefCen, 
          lb = lbCenBefore, 
          ub = ubCenBefore,
          ylb = -1, 
          yub = 1,  
          dvName = 'Federal Deputy')
plotTtest(betas = coefDec, 
          lb = lbDecBefore, 
          ub = ubDecBefore,
          ylb = -1, 
          yub = 1,  
          dvName = 'Federal Deputy')
dev.off()

